package chpt05;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;

import java.util.Locale;

public class DateTimeAPITest {

	static void println(Object e) {
		System.out.println(e);
	}

	static void printf(String p, Object... e) {
		System.out.printf(p, e);
	}

	public static void main(String[] args) {

		println("---演示1：LocalDateTime---");
		LocalDateTime now = LocalDateTime.now();
		int year = now.getYear();
		Month month = now.getMonth();
		int doy = now.getDayOfYear();
		printf("现在是%s年%s月%s日%n", year, month, doy);
		

		println("---演示2：LocalDate---");
		LocalDate date2 = LocalDate.of(2023, 2, 5);
		LocalDate date1 = date2.minusYears(3);
		LocalDate date3 = date2.plusDays(365);
		LocalDate date4 = date2.withMonth(10); // 将月份改为10
		println("date1=" + date1 + " date2=" + date2);
		println("date3=" + date3 + " date4=" + date4);
		boolean a = date2.isAfter(date1); // true
		boolean b = date2.isBefore(date3); // true
		boolean leapYear = date1.isLeapYear(); // true
		int num = date2.lengthOfYear(); // 获取date2当年天数
		DayOfWeek dow = date2.getDayOfWeek(); // 获取星期几
		println("date2 在 date1前吗? " + a);
		println("date3 在 date2后吗? " + b);
		println(date2 + "是闰年吗？" + leapYear);
		println(date2 + "有" + num + "天");
		println(date2 + "是" + dow.getDisplayName(TextStyle.FULL, Locale.getDefault()));
		// 通过date1中获得的年月来创建YearMonth实例
		YearMonth yearMonth = YearMonth.of(date1.getYear(), date1.getMonth());
		System.out.println(yearMonth + "有" + yearMonth.lengthOfMonth() + "天");
		long daysAfterEpooch = date2.toEpochDay(); // 获取自GMT 1970-01-01以来经过的天数
		printf("自纪元日%s后过去了%s天%n", LocalDate.EPOCH, daysAfterEpooch);
		long weeks = ChronoUnit.WEEKS.between(LocalDate.ofEpochDay(0), date2);
		println("纪元日与date2之间相差星期数为：" + weeks);
		long days = ChronoUnit.DAYS.between(date2, date3);
		System.out.println("date3与date2之间相差天数：" + days);
		println("2022年是闰年吗？" + Year.isLeap(2022));
		long until = date1.until(date4, ChronoUnit.YEARS); //ChoronoUnit为时间单位
		System.out.println("date1与 date4之间相差"+until+"年！");

		println("---演示3：LocalTime演示---");
		// LocalTime演示1
		LocalTime time1 = LocalTime.of(12, 0, 0);
		LocalTime time2 = time1.plusMinutes(3);
		System.out.println("time1：" + time1);
		System.out.println("time2：" + time2);
		long seconds = ChronoUnit.SECONDS.between(time2, time1);
		println("time1与time2之间相差秒数为: "+ seconds);
		
		// LocalTime演示2
		LocalDateTime now2 = LocalDateTime.now();
		LocalTime time = now2.toLocalTime();             // 获取now2的时间部分
		LocalDate date = now2.toLocalDate().plusDays(1); // 获取now2的日期部分并加一天
		LocalDateTime dateTime21 = time.atDate(date);    // 将time与date组合起来
		println("现在时间：" + time);
		println(dateTime21 + " =>纳秒部分为" + time.getNano() + 
				", 秒部分为" + time.getSecond());
		println(time.toSecondOfDay()); // 当前时间为当天的第几秒
		println(LocalTime.MIDNIGHT + " " + LocalTime.NOON + " " + LocalTime.MAX);

	}

}
